package com.itheima.service.impl;

import com.itheima.common.utils.DateUtils;
import com.itheima.mapper.OrderSettingDao;
import com.itheima.pojo.OrderSetting;
import com.itheima.pojo.OrderSettingVO;
import com.itheima.service.OrderSettingService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.*;

@Service
public class OrderSettingServiceimpl implements OrderSettingService {

    @Autowired
    OrderSettingService orderSettingService;
    @Autowired
    private OrderSettingDao orderSettingDao;


    @Override
    public void editMNumberByDate(OrderSetting orderSetting) {

        LocalDate date = orderSetting.getOrderDate().toInstant().atZone(java.time.ZoneId.systemDefault()).toLocalDate();

        OrderSetting orderSetting1 = orderSettingDao.findOrderDate(date);
        if (orderSetting1 != null) {
            Map<String, Object> map = new HashMap<>();
            map.put("orderDate", date);
            map.put("number", orderSetting.getNumber());
            orderSettingDao.editMNumberByDate(map);
        }
        else {
            orderSettingDao.add(orderSetting);
        }
    }

    @Override
    public List<OrderSettingVO> getOrderSettingByMonth(String month)  {
        try {
            Date month1=DateUtils.parseString2Date(month, "yyyy-MM");
            month = DateUtils.parseDate2String(month1, "yyyy-MM");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        List<OrderSetting> orderSettings = orderSettingDao.getOrderSettingByMonth(month);
        List<OrderSettingVO> orderSettingVOList = new java.util.ArrayList<>();
        for (OrderSetting orderSetting : orderSettings) {
//          拷贝
            OrderSettingVO orderSettingVO = new OrderSettingVO();
            BeanUtils.copyProperties(orderSetting, orderSettingVO);
            String date1;
            try {
                date1 = DateUtils.parseDate2String(orderSetting.getOrderDate(),"dd");
                Integer date = Integer.parseInt(date1);
                orderSettingVO.setDate(date);

            } catch (Exception e) {
                throw new RuntimeException(e);
            }

            orderSettingVOList.add(orderSettingVO);
        }
        return orderSettingVOList;
    }

    @Override
    public void add(List<String[]> excelMessage)  {


        for (String[] strings : excelMessage) {

                OrderSetting orderSetting = new OrderSetting();
                try {
                    orderSetting.setOrderDate(DateUtils.parseString2Date(strings[0], "yyyy/MM/dd"));
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
                orderSetting.setNumber(Integer.parseInt(strings[1]));
                orderSetting.setReservations(0);
                orderSettingDao.add(orderSetting);

        }



    }
}
